//
//  LoginView.m
//  商城
//
//  Created by runone02 on 2018/8/28.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "LoginView.h"
#import "DCUpDownButton.h"
#import "AccountPsdView.h"
#import "VerificationView.h"


@interface LoginView()<UIScrollViewDelegate,UITextFieldDelegate>
/** 头部*/
@property (nonatomic,strong) UIView *TopView;
/** 退出*/
@property (nonatomic,strong) UIButton *outButton;
/** 标题*/
@property (nonatomic,strong) UIButton *titleButton;
/** 注册*/
@property (nonatomic,strong) UIButton *registerButton;
/** 背景 */
@property (nonatomic,strong) UIView *bgView;

/** 快速登录View*/
@property (nonatomic,strong) UIView *quickLogonVIew;
/** label*/
@property (nonatomic,strong) UILabel *quickLabel;
/** 微博*/
@property (nonatomic,strong) DCUpDownButton *weiboButton;
/** QQ*/
@property (nonatomic,strong) DCUpDownButton *QQButton;
/** 微信*/
@property (nonatomic,strong) DCUpDownButton *weixinButton;
/** 其他*/
@property (nonatomic,strong) DCUpDownButton *qitaButton;

/* 上一次选中的按钮 */
@property (strong , nonatomic)UIButton *selectBtn;
/* indicatorView */
@property (strong , nonatomic)UIView *indicatorView;
/* titleView */
@property (strong , nonatomic)UIView *titleView;
/* contentView */
@property (strong , nonatomic)UIScrollView *contentView;

/* 验证码 */
@property (strong , nonatomic)VerificationView *verificationView;

/** 密码*/
@property (nonatomic,strong) AccountPsdView *accountView;
@end

@implementation LoginView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    WEAKSELF
    self.TopView = [[UIView alloc]init];
    self.TopView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.TopView];
    [self.TopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(kTopBarHeight);
    }];
    
    self.outButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.outButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.outButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.outButton addTarget:self action:@selector(outButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.outButton.titleLabel.font = TITLE_FONT(30);
    [self.TopView addSubview:self.outButton];
    [self.outButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.TopView).with.offset((10));
        make.width.height.mas_equalTo(30);
        make.centerY.mas_equalTo(weakSelf.TopView).with.offset(5);
    }];
    
    self.titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.titleButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.titleButton.titleLabel.font = TITLE_FONT(30);
    [self.TopView addSubview:self.titleButton];
    [self.titleButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.width.height.mas_equalTo(30);
        make.centerX.mas_equalTo(weakSelf.TopView);
        make.centerY.mas_equalTo(weakSelf.TopView).with.offset(5);
    }];
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerButton addTarget:self action:@selector(registerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.registerButton.titleLabel.font = TITLE_FONT(30);
    [self.TopView addSubview:self.registerButton];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.height.mas_equalTo(30);
        make.centerY.mas_equalTo(weakSelf.TopView).with.offset(5);
        make.right.mas_equalTo(weakSelf.TopView).with.offset(-10);
    }];
    
//    /** 第三方登录*/
    self.quickLogonVIew = [[UIView alloc]init];
    [self addSubview:self.quickLogonVIew];
    [self.quickLogonVIew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(140);
    }];
    
    self.quickLabel = [[UILabel alloc]init];
    self.quickLabel.text = @"使用以下账号登录";
    self.quickLabel.font = TITLE_FONT(30);
    self.quickLabel.textAlignment = NSTextAlignmentCenter;
    self.quickLabel.textColor = [UIColor blackColor];
    [self.quickLogonVIew addSubview:self.quickLabel];
    [self.quickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.quickLogonVIew).with.offset(W_RATIO(30));
        make.height.mas_equalTo(20);
        make.left.right.mas_equalTo(weakSelf.quickLogonVIew);
    }];

    self.QQButton = [DCUpDownButton buttonWithType:UIButtonTypeCustom];
    [self.QQButton setTitle:@"QQ" forState:UIControlStateNormal];
    [self.QQButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.QQButton setImage:IMAGE(@"share_qq") forState:UIControlStateNormal];
    [self.QQButton addTarget:self action:@selector(QQButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.QQButton.titleLabel.font = TITLE_FONT(28);
    [self.quickLogonVIew addSubview:self.QQButton];
    [self.QQButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.quickLogonVIew.mas_centerX).with.offset(-5);

        make.width.mas_equalTo(ScreenW/4-10);
        make.top.mas_equalTo(weakSelf.quickLabel.mas_bottom).with.offset(10);
        make.bottom.mas_equalTo(weakSelf.quickLogonVIew.mas_bottom).with.offset(-10);
    }];
    
    self.weiboButton = [DCUpDownButton buttonWithType:UIButtonTypeCustom];
    [self.weiboButton setTitle:@"微博" forState:UIControlStateNormal];
    [self.weiboButton setImage:IMAGE(@"share_weibo") forState:UIControlStateNormal];
    [self.weiboButton addTarget:self action:@selector(weiboButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.weiboButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.weiboButton.titleLabel.font = TITLE_FONT(28);
    [self.quickLogonVIew addSubview:self.weiboButton];
    [self.weiboButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakSelf.QQButton.mas_left).with.offset(-10);
        
        make.width.mas_equalTo(ScreenW/4-10);
        make.top.mas_equalTo(weakSelf.quickLabel.mas_bottom).with.offset(10);
        make.bottom.mas_equalTo(weakSelf.quickLogonVIew.mas_bottom).with.offset(-10);
    }];
    
    self.weixinButton = [DCUpDownButton buttonWithType:UIButtonTypeCustom];
    [self.weixinButton setTitle:@"微信" forState:UIControlStateNormal];
    [self.weixinButton setImage:IMAGE(@"share_wx") forState:UIControlStateNormal];
    [self.weixinButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.weixinButton.titleLabel.font = TITLE_FONT(28);
    [self.weixinButton addTarget:self action:@selector(weixinButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.quickLogonVIew addSubview:self.weixinButton];
    [self.weixinButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.quickLogonVIew.mas_centerX).with.offset(5);
        
        make.width.mas_equalTo(ScreenW/4-10);
        make.top.mas_equalTo(weakSelf.quickLabel.mas_bottom).with.offset(10);
        make.bottom.mas_equalTo(weakSelf.quickLogonVIew.mas_bottom).with.offset(-10);
    }];
    
    self.qitaButton = [DCUpDownButton buttonWithType:UIButtonTypeCustom];
    [self.qitaButton setTitle:@"新浪" forState:UIControlStateNormal];
    [self.qitaButton setImage:IMAGE(@"share_sina") forState:UIControlStateNormal];
    [self.qitaButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.qitaButton.titleLabel.font = TITLE_FONT(28);
    [self.qitaButton addTarget:self action:@selector(qitaButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.quickLogonVIew addSubview:self.qitaButton];
    [self.qitaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.weixinButton.mas_right).with.offset(10);
        
        make.width.mas_equalTo(ScreenW/4-10);
        make.top.mas_equalTo(weakSelf.quickLabel.mas_bottom).with.offset(10);
        make.bottom.mas_equalTo(weakSelf.quickLogonVIew.mas_bottom).with.offset(-10);
    }];
    
    self.bosomView = [[UIView alloc]init];
    [self addSubview:self.bosomView];
    
    [self.bosomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(weakSelf.TopView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.quickLogonVIew.mas_top);
    }];
    
    _titleView = [UIView new];
    _titleView.frame = CGRectMake(0, 0, ScreenW, 35);
    [self.bosomView addSubview:_titleView];
    
    NSArray *titleArray = @[@"账号密码登录",@"短信验证登录"];
    CGFloat buttonW = (_titleView.dc_width - 30) / 2;
    CGFloat buttonH = _titleView.dc_height - 3;
    CGFloat buttonX = 15;
    CGFloat buttonY = 0;
    for (NSInteger i = 0; i < titleArray.count; i++) {
        
        UIButton *button = [UIButton  buttonWithType:UIButtonTypeCustom];
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = TITLE_FONT(30);
        button.tag = i;
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.frame = CGRectMake((i * buttonW) + buttonX, buttonY, buttonW, buttonH);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:button];
    }
    
    UIButton *firstButton = _titleView.subviews[0];
    [self buttonClick:firstButton];
    
    _indicatorView = [UIView new];
    _indicatorView.backgroundColor = [firstButton titleColorForState:UIControlStateSelected];
    
    [firstButton.titleLabel sizeToFit];
    
    _indicatorView.dc_height = 2;
    _indicatorView.dc_width = firstButton.titleLabel.dc_width;
    _indicatorView.dc_centerX = firstButton.dc_centerX;
    _indicatorView.dc_y = _titleView.dc_bottom - _indicatorView.dc_height;
    [_titleView addSubview:_indicatorView];
    
    self.contentView = [[UIScrollView alloc]init];
    self.contentView.contentSize = CGSizeMake(ScreenW * titleArray.count, 0);
    
    [self.bosomView addSubview:_contentView];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.selectBtn.mas_bottom).with.offset(5);
          make.left.right.bottom.mas_equalTo(weakSelf.bosomView);
    }];
    
    _verificationView = [[VerificationView alloc]init];
    [_contentView addSubview:_verificationView];
    
    _accountView = [[AccountPsdView alloc]init];
    [_contentView addSubview:_accountView];
    
    [_accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.contentView).with.offset(10);
        make.left.mas_equalTo(weakSelf.contentView);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.mas_equalTo(weakSelf.contentView);
    }];
    
    
    [_verificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.accountView);
        make.left.mas_equalTo(weakSelf.accountView.mas_right);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.mas_equalTo(weakSelf.accountView);
    }];
}

- (void)outButtonClick:(UIButton *)btn{
    if (_outButtonBlock) {
        _outButtonBlock();
    }
}

- (void)registerButtonClick:(UIButton *)btn{
    if (_registerButtonBlock) {
        _registerButtonBlock();
    }
}

- (void)QQButtonClick:(UIButton *)btn{
    if (_QQButtonBlock) {
        _QQButtonBlock();
    }
}

- (void)weiboButtonClick:(UIButton *)btn{
    if (_weiboButtonBlock) {
        _weiboButtonBlock();
    }
}

- (void)weixinButtonClick:(UIButton *)btn{
    if (_weixinButtonBlock) {
        _weixinButtonBlock();
    }
}

- (void)qitaButtonClick:(UIButton *)btn{
    if (_qitaButtonBlock) {
        _qitaButtonBlock();
    }
}

//- (UIScrollView *)contentView{
//    if (!_contentView) {
//        _contentView = [[UIScrollView alloc] initWithFrame:CGRectZero];
//        _contentView.delegate = self;
//        _contentView.userInteractionEnabled = YES;
//    }
//    return _contentView;
//}



#pragma mark - 按钮点击
- (void)buttonClick:(UIButton *)button{
    button.selected = !button.selected;
    [_selectBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _selectBtn = button;
    
    WEAKSELF
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.indicatorView.dc_width = button.titleLabel.dc_width;
        weakSelf.indicatorView.dc_centerX = button.dc_centerX;
    }];
    
    CGPoint offset = _contentView.contentOffset;
    offset.x = _contentView.dc_width * button.tag;
    [_contentView setContentOffset:offset animated:YES];
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / scrollView.dc_width;
    UIButton *button = _titleView.subviews[index];
    
    [self buttonClick:button];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

@end
